package com.hymjweb.demo.hymj.basic_data_manage.xt_dic.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.bean.XtDicPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.service.XtDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/pages/basic_data_manage/xt_dic")
public class XtDicController extends BaseController {

    @Autowired
    private XtDicService xtDicService;


    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/basic_data_manage/xt_dic/xtdic";
    }

    @RequestMapping("/querydroplist")
    @ResponseBody
    public Object querydroplist() {
        return xtDicService.findCacheAll();
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = true) boolean isLeft, @RequestParam(required = false) Map param) throws Exception {
        if (param == null && param.isEmpty()) {
            throwException("数据字典查询参数错误");
        }
        param.put("isLeft", isLeft);
        return xtDicService.getPageList(param);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("数据字典新增参数错误,新增失败");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("数据字典新增成功!");

        XtDicPojo pojo = xtDicService.bulidPojo(param);
        int affectRows = xtDicService.insert(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("数据字典新增失败!");
        }
        return baseResultMap;
    }


    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("数据字典更新参数错误,更新失败");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("数据字典更新成功!");

        XtDicPojo pojo = xtDicService.bulidPojo(param);
        int affectRows = xtDicService.update(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("数据字典更新失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestParam(required = true) Map map) throws Exception {
        if(map==null || map.isEmpty()){
            throwException("数据字典删除参数错误,删除失败");
        }

        String id = DataTypeUtil.toNotNullString(map.get("id"));
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("数据字典删除参数错误,删除失败");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("数据字典删除成功!");

        int affectRows = xtDicService.delete(id);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("数据字典删除失败!");
        }
        return baseResultMap;
    }

}
